/**  
 * @Title:  GlobalExceptionHandler.java   
 * @Package com.platform.exception    
 * @author: <a href="mailto:15909910367@163.com">吴先生</a> 
 * @date:   2022年10月28日 下午4:29:20   
 * @version V1.0 
 * @Copyright: 2022 吴先生.
 */
package com.platform.exception;

import static com.platform.utils.StrPool.EMPTY;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.platform.base.R;
import com.platform.exception.code.ExceptionCode;
import com.platform.logger.event.ErrorLogEvent;
import com.platform.logger.model.ErrorLogModel;
import com.platform.logger.publisher.ErrorLogPublisher;

import lombok.extern.log4j.Log4j;


/**   
 * @ClassName:  GlobalExceptionHandler   
 * @Description: 异常中央处理
 * @date:   2022年10月28日 下午4:29:20   
 * @author: <a href="mailto:15909910367@163.com">吴先生</a>  
 * @Copyright: 2022 吴元森.  
 */
@Log4j
@Configurable
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@SuppressWarnings("unused")
public class SingleExceptionHandler extends com.platform.handler.GlobalExceptionHandler{

	@ExceptionHandler(Throwable.class)
    public R<String> otherExceptionHandler(Throwable ex, HttpServletRequest request) {
        ErrorLogPublisher.event(new ErrorLogEvent(new ErrorLogModel()), ex).publish();
        ex.printStackTrace();
        return R.result(ExceptionCode.INTERNAL_SERVER_ERROR.getCode(), EMPTY, ExceptionCode.INTERNAL_SERVER_ERROR.getMsg()).setPath(request.getRequestURI());
    }
	
	@ExceptionHandler(ArgumentException.class)
	public R<String> onArgumentException(ArgumentException ex, HttpServletRequest request){
		return R.result(ExceptionCode.NULL_POINT_EX.getCode(), EMPTY, ex.getMessage()).setPath(request.getRequestURI());
	}
}
